package test;

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;

public class OTable {   //处理文档中表格的问题，读取，替换等
	
	private List<String> tableData; //记录每个table的数据
	private HWPFDocument doc;
	
	public OTable(HWPFDocument document){
		tableData  =  new LinkedList<>();
		this.doc = document;
		readTableMsg();
	}
	
	private void readTableMsg(){  //读取文档中所有table信息
		Range range = doc.getRange(); //Range层
		TableIterator tab = new TableIterator(range);   
		while(tab.hasNext()){
			Table table = tab.next();
			readTable(table);
		}
	}
	
	public  String readTable(Table tab){ //获得具体某个table的内容
		String res = "";
		for (int i = 0; i < tab.numRows(); i++) {     
            TableRow tr = tab.getRow(i);     
            //迭代列，默认从0开始  
            for (int j = 0; j < tr.numCells(); j++) {     
                TableCell td = tr.getCell(j);//取得单元格  
                //取得单元格的内容  
                for(int k=0;k<td.numParagraphs();k++){ 
                	//System.out.println("k"+td.numParagraphs());
                	Paragraph paragraph =td.getParagraph(k);
                    res =res+ paragraph.text()+"\u0005"+"@TD@"; //\u0007列分隔符  
                }
            }
            res = res+"@TR@"; //行分隔符
         }
		res = res.replaceAll("(\u0013.{1,30}\u0015)+","\u0002");
		tableData.add(res);
		return res;
	}
	
	public List<String> getTableData(){  //返回文档中所有table的数据，一个table为一个集合一个元素
		return tableData;
	}
	
	public String replaceXmlTable(String xmlData){    //将数据中的 table 替换为xml格式：将表格标记换成对应表格内容，并在表格前位加入标记，标记此处是表格
		String xml = xmlData.replaceAll("@TABLE@", "\u0003@TABLE@");
		Matcher mm = Pattern.compile("@TABLE@").matcher(xml);
		int index= 0;
		while(mm.find() && index<tableData.size()){
			xml = xmlData.replaceFirst("@TABLE@", xmlTable(tableData.get(index++)));
		}
		return xml;
	}
	
	public String replaceHtmlTable(String htmlData){   //将所有table标记还原成table
		Matcher mm = Pattern.compile("@TABLE@").matcher(htmlData);
		int index= 0;
		while(mm.find() && index<tableData.size()){
			htmlData = htmlData.replaceFirst("@TABLE@", htmlTable(tableData.get(index++)));
		}
		return htmlData;
	}
	
	public String htmlTable(String table){ //将某个table数据解析成html格式table
		String res = "";
		res = "<table border='1'>";
		String[] tr = table.split("@TR@"); //table按行拆分存储;
		for(int j=0;j<tr.length;j++){
			String[] td = tr[j].split("@TD@");
			res = res+"<tr>";
			for(int k=0;k<td.length;k++){
				res = res+"<td>";
				res = res+td[k];
				res = res+"</td>";
			}
			res = res+"</tr>";
		}
		res = res+"</table>";
		return res;
}

	public String xmlTable(String table){ //将某个table数据解析成xml格式table
		String res = "";
		res = "\t\t<table border='1'>\n";
		String[] tr = table.split("@TR@"); //table按行拆分存储;
		for(int j=0;j<tr.length;j++){
			String[] td = tr[j].split("@TD@");
			res = res+"\t\t\t<tr>\n";
			for(int k=0;k<td.length;k++){
				res = res+"\t\t\t\t<td>";
				res = res+td[k];
				res = res+" </td>\n";
			}
			res = res+"\t\t\t</tr>\n";
		}
		res = res+"\t\t</table>\n";
		return res;
	}
	
}
